#!/bin/bash

#配置mysql
echo "====== 启动 mysql ====="
mysql_install_db --user=mysql --explicit_defaults_for_timestamp
service mysql start
echo "====== 启动完成 ========"


#项目配置
echo "********** 进入项目目录 *************"
if [[ $USC_ROOT_PATH != "" ]]; then
	echo "USC_ROOT_PATH: $USC_ROOT_PATH"
    sed -r -i "s/#(host): (.*)/\1=$USC_ROOT_PATH/g" /var/www/humble_openid_server/config/database.yml
fi
cd /var/www/humble_openid_server/
echo "********** 编译 *************"
/bin/bash -l -c "bundle install"
echo "********** 创建数据库 *************"
/bin/bash -l -c "rake db:create RAILS_ENV=production"
/bin/bash -l -c "rake db:migrate RAILS_ENV=production"
/bin/bash -l -c "rake db:seed RAILS_ENV=production"
echo "********** 创建完成 *************"

echo "********** 启动 Nginx 中，请稍后 *************"
/opt/nginx/sbin/nginx -g "daemon off;"

echo "********** Good Job, 启动完成 *************"